﻿/*======================================
作者：洞庭夕照
创建：2017.10.12
网站：www.ninesky.cn
      mzwhj.cnblogs.com
支持：bbs.ninesky.cn
代码：git.oschina.net/ninesky/Ninesky
版本：v1.0.0.0
======================================*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Ninesky.Models;
using Ninesky.IService;
using Ninesky.MVC.ViewModels;


namespace Ninesky.MVC.Areas.Member.Controllers
{
    /// <summary>
    /// 内容管理
    /// </summary>
    [Area("Member")]
    public class ContentController : Controller
    {
        private IContentService _contentService { get; set; }
        public ContentController(IContentService contentService)
        {
            _contentService = contentService;
        }
        // GET: /<controller>/
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 列表页
        /// </summary>
        /// <returns></returns>
        public IActionResult List(int id = 0, ModuleType type = 0)
        {
            ViewData["id"] = id;
            ViewData["type"] = type;
            return View();
        }
        #region Json

        [HttpPost]
        public async Task<IActionResult> DeleteJson(int id)
        {
            var opResult = await _contentService.RemoveAsync(new Content() { ContentId = id });
            return new JsonResult(opResult);
        }
        public async Task<IActionResult> ListJson(int categoryid = 0, ModuleType type = 0, int pageSize = 20, int pageIndex = 1)
        {
            Paging<ContentItem> paging = new Paging<ContentItem>() { PageIndex = pageIndex, PageSize = pageSize };
            paging = await _contentService.FindPagingAsync(categoryid, type, string.Empty, string.Empty, null, ContentOrder.IdDesc, paging);
            return new JsonResult(new { total = paging.Total, rows = paging.Entities });
        }
        #endregion

        #region 添加

        /// <summary>
        /// 添加内容
        /// </summary>
        /// <param name="id">栏目Id</param>
        /// <param name="type">内容类型</param>
        /// <returns></returns>
        public async Task<IActionResult> Add([FromServices]ICategoryService categoryService, int? id, ModuleType? type)
        {
            if (id == null)
            {
                if (type != null && Enum.IsDefined(typeof(ModuleType), type))
                {
                    switch (type)
                    {
                        case ModuleType.Article:
                            return View("AddArticle");
                    }
                }
            }
            else
            {
                var category = await categoryService.FindAsync((int)id);
                if (category == null)
                {
                    var notice = new NoticeViewModel() { Type = NoticeType.Failure, Tile = "错误", Message = "指定的栏目不存在", Links = new Dictionary<string, string>() };
                    notice.Links.Add("返回", Url.Action("Add", "Content"));
                    return View("Notice", notice);
                }
                else
                {
                    switch (category.ModuleType)
                    {
                        case ModuleType.Article:
                            return View("AddArticle", new Content { CategoryId = category.CategoryId, ModuleType = ModuleType.Article });
                    }
                }
            }

            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Add(Content content)
        {

            if (ModelState.IsValid)
            {

                switch (content.ModuleType)
                {
                    //文章
                    case ModuleType.Article:
                        NoticeViewModel notice;
                        if (content.Article == null)
                        {
                            notice = new NoticeViewModel() { Type = ViewModels.NoticeType.Failure, Tile = "错误", Message = "文章数据不存在。" };
                            return View("Notice", notice);
                        }
                        else
                        {
                            content.CreateTime = System.DateTime.Now;
                            var opsResult = await _contentService.AddAsync(content);
                            if (opsResult.Succeed == false) ModelState.AddModelError("", opsResult.Message);
                            else
                            {
                                notice = new NoticeViewModel { Type = NoticeType.Succeed, Tile = "添加成功", Message = "您已成功添加了【" + content.Title + "】", Links = new Dictionary<string, string>() };
                                notice.Links.Add("继续添加", Url.Content("~/Member/Content/Add/" + content.CategoryId));
                                notice.Links.Add("修改内容", Url.Content("~/Member/Content/Modify/" + content.ContentId));
                                notice.Links.Add("浏览内容", Url.Content("~/Member/Content/List"));
                                return View("Notice", notice);
                            }
                        }
                        break;
                    default:
                        notice = new NoticeViewModel { Type = ViewModels.NoticeType.Failure, Tile = "错误", Message = "内容模型错误。" };
                        break;
                }
            }
            switch (content.ModuleType)
            {
                //文章
                case ModuleType.Article:
                    return View("AddArticle", content);


                default:
                    return View(new Category() { CategoryId = content.CategoryId });
            }
        }
        #endregion

        #region 修改
        public async Task<IActionResult> Modify(int id)
        {
            var content = await _contentService.FindAsync(id);
            if (content == null)
            {
                NoticeViewModel notice = new  NoticeViewModel { Type = NoticeType.Failure, Tile = "错误", Message = "内容不存在。" };
                return View("Notice", notice);
            }
            else
            {
                switch (content.ModuleType)
                {
                    //文章
                    case ModuleType.Article:
                        return View("ModifyArticle", content);
                }
            }
            return View(content);
        }

        [HttpPost]
        public async Task<IActionResult> Modify(Content content)
        {
            if (ModelState.IsValid)
            {
                switch (content.ModuleType)
                {
                    //文章
                    case ModuleType.Article:
                        NoticeViewModel notice;
                        if (content.Article == null)
                        {
                            notice = new NoticeViewModel { Type = ViewModels.NoticeType.Failure, Tile = "错误", Message = "文章数据不存在。" };
                            return View("Notice",notice);
                        }
                        else
                        {
                            var opsResult = await _contentService.UpdateAsync(content);
                            if (opsResult.Succeed == false) ModelState.AddModelError("", opsResult.Message);
                            else
                            {
                                notice = new NoticeViewModel { Type = NoticeType.Succeed, Tile = "修改成功", Message = "您已成功修改了【" + content.Title + "】", Links = new Dictionary<string, string>() };
                                notice.Links.Add("添加文章", Url.Content("~/Member/Content/Add/" + content.CategoryId));
                                notice.Links.Add("修改文章", Url.Content("~/Member/Content/Modify/" + content.ContentId));
                                notice.Links.Add("内容列表", Url.Content("~/Member/Content/List/"));
                                return View("Notice", notice);
                            }
                        }
                        break;
                    default:
                        notice = new NoticeViewModel { Type = ViewModels.NoticeType.Failure, Tile = "错误", Message = "内容模型错误。" };
                        return View("Notice", notice);
                }
            }
            switch (content.ModuleType)
            {
                //文章
                case ModuleType.Article:
                    return View("AddArticle", content);
                default:
                    return View(content);
            }
        }
#endregion
    }
}
